package org.restorator.dao.implement;

import java.util.ArrayList;
import java.util.List;

import org.restorator.dao.base.TableDao;
import org.restorator.entity.AnonymTable;
import org.restorator.entity.Table;
import org.restorator.exception.DataStoreException;

public class TableDaoMock implements TableDao {

	private List<Table> list = new ArrayList<Table>();

	private static TableDaoMock dao = null;

	private TableDaoMock() {
		list.add(new AnonymTable(1, 10, 1, "empty"));
		list.add(new AnonymTable(2, 20, 2, "empty"));
		list.add(new AnonymTable(3, 10, 2, "ordered"));
		list.add(new AnonymTable(4, 50, 1, "ordered"));
		list.add(new AnonymTable(5, 40, 2, "empty"));
	}
	
	public static TableDaoMock newInstance() {
		if (dao == null) {
			dao = new TableDaoMock();
		}
		return dao;
	}

	@Override
	public Table get(Integer id) throws DataStoreException {
		for (Table li : list) {
			if (li.getTableID() == id) {
				return li;
			}
		}
		return null;
	}

	@Override
	public Table update(Table table) {
		for (Table li : list) {
			if (li.getTableID() == table.getTableID()) {
				li.setOrder(table.getOrder());
				li.setStatus(table.getStatus());
				li.setTableID(table.getTableID());
				li.setX(table.getX());
				li.setY(table.getY());
			}
		}		
		return table;
	}

	@Override
	public long count() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public Integer save(Table table, Object... addParams)
				throws DataStoreException {
		list.add(table);
		return null;
	}

	@Override
	public List<Table> getAll() throws DataStoreException {
		if (list.isEmpty()) {
			return null;
		}
		return list;
	}

	@Override
	public boolean exist(Integer id) {
		for (Table li : list) {
			if (li.getTableID() == id) {
				return true;
			}
		}
		return false;
	}

	@Override
	public void delete(Table table) {
		for (Table li : list) {
			if (li.getTableID() == table.getTableID()) {
				list.remove(table);
			}
		}
	}
}
